import {Injectable} from "@angular/core";
import {CodePush} from "@ionic-native/code-push";
import {Events, Platform} from "ionic-angular";
import {DialogProvider} from "@providers/dialog";
import {Device} from "@ionic-native/device";
import {Util} from "@providers/util";
import {AppUpdate} from "@ionic-native/app-update";
import {env} from "@env/environment";

@Injectable()
export class UpdateProvider {

  /**
   * 控制是否需要再次请求框架更新
   */
  canAppUpdate: boolean = Boolean(localStorage.getItem("canAppUpdate")) || true;

  constructor(
    private dialog: DialogProvider,
    private platform: Platform,
    private device: Device,
    private appUpdate: AppUpdate,
    private events: Events,
    private codePush: CodePush) {
  }

  /**
   * 检查热更新检查
   */
  checkCodePush(isMsg: boolean = false): void {
    this.platform.ready().then(() => {
      //浏览器不执行以下代码
      if (!this.platform.is("cordova")) {
        if (isMsg) {
          this.dialog.showAlert("浏览器访问此功能无效");
        }
        return;
      }

      //通过平台，确定热更新密钥
      let platform = this.device.platform;
      let codePush_key;
      switch (platform) {
        case "Android":
          codePush_key = env.CodePushDeploymentKey_ANDROID;
          break;
        case "iOS":
          codePush_key = env.CodePushDeploymentKey_IOS;
          break;
        default:
          codePush_key = env.CodePushDeploymentKey_ANDROID
      }

      if (Util.isNull(codePush_key)) {
        if (isMsg) {
          this.dialog.showAlert("此项目没有配置热更新");
        }
        return;
      }

      //检查是否存在可用的更新
      this.codePush.checkForUpdate(codePush_key).then(iRemotePackage => {
        if (!iRemotePackage) {
          if (isMsg) this.dialog.showAlert("已经是最新版本");
          return;
        }
        this.events.publish("update:open", iRemotePackage);
      }).catch(e => {
        // this.dialog.showAlert(e)
      });
    })
  }

  /**
   * 外壳更新
   */
  checkAppUpdate(isMsg: boolean = false): void {
    this.platform.ready().then(() => {
      //浏览器不执行以下代码
      if (!this.platform.is("cordova")) {
        if (isMsg) {
          this.dialog.showAlert("浏览器访问此功能无效");
        }
        return;
      }
      let configXml = env.APP_UPDATE_XML;
      if (Util.isNull(configXml)) return;
      if (!this.appUpdate) return;
      this.appUpdate.checkAppUpdate(configXml).then(code => {
        switch (code) {
          case 601: {
            //如果提示没有权限，则弹框提示用户是否需要不再提示
            this.dialog.showConfirm(() => {
              this.canAppUpdate = false;
              localStorage.setItem("canAppUpdate", "false");
            }, '更新权限获取失败，点击确定不再提示更新', '提示',)
            break;
          }
          case 202: {
            if (isMsg) this.dialog.showAlert("已经是最新版本");
            break;
          }
        }
      });
    });
  }

  timer() {
    this.update();
    setInterval(() => {
      this.update();
    }, 1000 * 60 * 5);
  }

  update(isMsg: boolean = false) {
    this.platform.ready().then(() => {
      //浏览器不执行以下代码
      if (this.platform.is("cordova")) {
        this.checkCodePush(isMsg);
        this.checkAppUpdate(isMsg);
      }
    })
  }

}
